package handler

import (
	"encoding/json"

	db "github.com/treelab/updatecolumn/data"
	"github.com/treelab/updatecolumn/models"
	pb "github.com/treelab/updatecolumn/protos"
)

//validateColumnExist for some table
func validateColumnExist(workerSpaceID, coreID, tableID, columnID string) (models.Column, bool) {

	var result db.GetColumnExistQueryResult

	resp, err := db.Query(db.GetColumnExistQueryString(workerSpaceID, coreID, tableID, columnID))

	if err != nil {
		return models.Column{}, false
	}

	err = json.Unmarshal(resp.GetJson(), &result)

	if err != nil || len(result.Workspaces) == 0 || result.Workspaces[0].UID != columnID {
		return models.Column{}, false
	}
	return result.Workspaces[0].Cores[0].Tables[0].Columns[0], true
}

//checkTableFormulaColumnsExists of some table
func checkTableFormulaColumnsExists(columnConfig *pb.ColumnConfig) bool {

	return false
}

//startToCovertColumnTo
func startToCovertColumn(in *pb.UpdateRequest, targetTypes pb.ColumnType) {

	switch targetTypes {
	case pb.ColumnType_Text:
	case pb.ColumnType_LongText:
	case pb.ColumnType_DateTime:
	case pb.ColumnType_RecordReference:
	case pb.ColumnType_Currency:
	case pb.ColumnType_Number:
	case pb.ColumnType_CheckBox:
	case pb.ColumnType_MultSelect:
	case pb.ColumnType_Select:
	case pb.ColumnType_Formula:
	case pb.ColumnType_MultiAttachments:
	case pb.ColumnType_LookUp:
	case pb.ColumnType_RollUp:
	case pb.ColumnType_UniqueID:
	case pb.ColumnType_Rating:
	case pb.ColumnType_Phone:
	case pb.ColumnType_Email:
	case pb.ColumnType_Collaborator:
	default:
		return
	}

	//5、ConvertColumn_v2 真正的column转换逻辑
	ConvertColumn()

}

//getAllreferencedColumnByColumnIds is big query
func getAllreferencedColumnByColumnIds() {

}

// updateColumnCellRelationships update all relationship
func updateColumnCellRelationships() {

}

func ConvertColumn() {

}

//updateFormView
func updateFormView() {

}
